﻿using System;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace KBE_HotFix.UI
{
    public class UIHandler
    {
        
        public static Dictionary<string, UIHandler> UIInstances = new Dictionary<string, UIHandler>();

        public UIHandler()
        {
            string clsName = GetType().Name;
            if (!UIInstances.ContainsKey(clsName))
            {
                UIInstances.Add(clsName, this);
            }
        }

        public static object DoGetHotFixCls(string handleCls)
        {
            UIHandler uiInstance = null;
            UIInstances.TryGetValue(handleCls, out uiInstance);
            if (uiInstance == null)
            {
                var clsType = Type.GetType(NamespacePrefix.UI + handleCls);
                if (clsType == null)
                {
                    string msg = "cant find class " + handleCls;
                    KBEngine.Dbg.ERROR_MSG(msg);
                    return null;
                }
                uiInstance = (UIHandler)Activator.CreateInstance(clsType);
            }
            return uiInstance;
        }

        //public static void DoBindEventHandlers(string handleCls, string handler, UIBehaviour.UIActionType type, UIEventListener listener)
        //{
        //  var uiInstance = DoGetHotFixCls(handleCls);
        //  var methodInfo = uiInstance.GetType().GetMethod(handler);
        //  Dbg.DEBUG_MSG(methodInfo.Name);
        //  Dbg.DEBUG_MSG(handleCls + " : " + handler);
        //  Dbg.DEBUG_MSG(typeof(UIEventListener.VoidDelegate).FullName);
        //  Dbg.DEBUG_MSG(typeof(VoidDelegate).FullName);
        //  var method1 = Delegate.CreateDelegate(typeof(GameObjTest), UIInstances["UI"], "GetTest", false);

        //  var method = Delegate.CreateDelegate(typeof(UIEventListener.VoidDelegate), uiInstance, handler, false);
        //  bindMethod(type, method, listener);
        //}

        //private static void bindMethod(UIBehaviour.UIActionType type, Delegate handler, UIEventListener listener)
        //{
        //  switch (type)
        //  {
        //    case UIBehaviour.UIActionType.SUBMIT:
        //      listener.onSubmit = (UIEventListener.VoidDelegate)handler;
        //      break;
        //    case UIBehaviour.UIActionType.CLICK:
        //      listener.onClick = (UIEventListener.VoidDelegate)handler;
        //      break;
        //    case UIBehaviour.UIActionType.DOUBLE_CLICK:
        //      listener.onDoubleClick = (UIEventListener.VoidDelegate)handler;
        //      break;
        //    default:
        //      string msg = "UIActionType Error: type is " + type;
        //      KBEngine.Dbg.ERROR_MSG(msg);
        //      break;
        //  }
        //}

        public void RegisterOut(string name, string method)
        {
            KBE_HotFix.Event.registerOut(name, this, method);
        }

        public void DeregisterAll()
        {
            KBE_HotFix.Event.deregisterOut(this);

        }
    }
}
